package com.wanxi.step07test.controller;

import com.wanxi.step07test.dto.ResultDTO;
import com.wanxi.step07test.model.LoginParams;
import com.wanxi.step07test.model.SUser;
import com.wanxi.step07test.model.UserModel;
import com.wanxi.step07test.service.SUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@CrossOrigin
@RestController
@Slf4j
public class SUserController {


    @Autowired
    SUserService sUserService;





    // 进行用户的认证，获取jwt token
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResultDTO login(LoginParams loginParams){
        ResultDTO resultDTO = sUserService.login(loginParams);
        return resultDTO;

//        String code = loginParams.getCode();
//
//        String uuid = loginParams.getUuid();
//
//        String vCode = (String) redisTemplate.opsForValue().get(uuid);
//
//
//        if(!code.equals(vCode)){
//            return ResultDTO.builder()
//                    .code(0)
//                    .msg("error-code")
//                    .build();
//        }
//
////        try {
////            vCode.equals(loginParams.getCode());
////        }catch (Exception e){
////            e.printStackTrace();
////            return ResultDTO.builder()
////                    .code(0)
////                    .msg("error-code")
////                    .build();
////        }
//
//
//
//
//        HashMap<String, String> data = new HashMap<>();
//        String token = null;
//        try {
//            token = sUserService.login(loginParams);
//            log.info("看这里的token: {}", token);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return ResultDTO.builder()
//                    .code(0)
//                    .msg("error")
//                    .build();
//        }
//        if (StringUtils.isEmpty(token)){
//            return ResultDTO.builder()
//                    .code(0)
//                    .msg("error")
//                    .build();
//        }
//
//        data.put("tokenHead",tokenHead);
//        data.put("access_token",token);
//        // localStorage.setItem("Authorization","Bearer sdsdfdfds")
//        // $ajax{data:{},type:"",header:{"Authorization":"Bearer sdsdfdfds"}}
//        return ResultDTO.builder()
//                .code(0)
//                .msg("success")
//                .data(data)
//                .build();
    }


    @PreAuthorize("hasAuthority('admin:add')")
    @RequestMapping("/back/admin/add")
    public ResultDTO add(SUser sUser) {
        ResultDTO resultDTO = sUserService.add(sUser);
        return resultDTO;
    }

    @PreAuthorize("hasAuthority('admin:findAll')")
    @RequestMapping("/back/admin/findAll")
    public ResultDTO findAll(SUser sUser) {
        ResultDTO resultDTO = sUserService.findAll(sUser);
        return resultDTO;
    }

    @PreAuthorize("hasAuthority('admin:delete')")
    @RequestMapping("/back/admin/delete")
    public ResultDTO delete(SUser sUser) {
        ResultDTO resultDTO = sUserService.delete(sUser);
        return resultDTO;
    }

    @PreAuthorize("hasAuthority('admin:findById')")
    @RequestMapping("/back/admin/findById")
    public ResultDTO findById(Integer id) {
        ResultDTO resultDTO = sUserService.findById(id);
        return resultDTO;
    }

    @PreAuthorize("hasAuthority('admin:update')")
    @RequestMapping("/back/admin/update")
    public ResultDTO update(SUser sUser) {
        ResultDTO resultDTO = sUserService.update(sUser);
        return resultDTO;
    }

    @PreAuthorize("hasAuthority('admin:updatePassword')")
    @RequestMapping("/back/admin/updatePassword")
    @ResponseBody
    public ResultDTO password(SUser sUser) {
        ResultDTO resultDTO = sUserService.updatePassword(sUser);
        return resultDTO;
    }





//    @PreAuthorize("hasAuthority('wx:product:read')")
//    @PostMapping("/test")
//    public CommonResult test(Authentication authentication){
//        SecurityContext securityContext = SecurityContextHolder.getContext();
////        Authentication authentication2 = securityContext.getAuthentication();
//        return CommonResult.success(securityContext);
//    }

    //todo  注册
    //1 接受账号密码
    //2 查询是有存在重复账号
    //3 密码加密
    //4 保存到数据库


//    @RequestMapping("/users")
//    public String getAllUsers() {
//        log.info("已访问所有用户....");
//        return "已访问所有用户....";
//    }
//
//    @PostMapping("/index")
//    public CommonResult index(@RequestBody LoginParams loginParams){
//        HashMap<String, String> data = new HashMap<>();
//        return CommonResult.success(data);
//    }

}


//    @RequestMapping(value = "/login", method = RequestMethod.POST)
//    public String login(String username, String password) {
//
//        //1. 根据用户名和密码验证该用户是否是合法用户
//
//        WxUser wxUser = (WxUser) userDetailsService.loadUserByUsername(username);
//        // 把输入的密码加密后进行比对
//        if (!this.passwordEncoder.matches(password, wxUser.getPassword())) {
//            // 抛出错误或其他
//
//        } else {
//            //
//
//            UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken
//                    = new UsernamePasswordAuthenticationToken(wxUser,null,wxUser.getAuthorities());
//            SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);
//        }
//        return "登录";
//        // 2. 验证通过后，创建一个UsernameAndPasswordAuthenticationToken对象
//
//        // 3. 把这个对系那个交给框架
//    }
//}
